package com.chengyu.core.interceptor;

import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.UmsMember;
import com.chengyu.core.model.UmsShopAccount;
import com.chengyu.core.service.member.MemberService;
import com.chengyu.core.service.shop.ShopAccountService;
import com.chengyu.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component //将该类注入到容器中
public class FreezeInterceptor implements HandlerInterceptor {
    @Autowired
    protected ShopAccountService shopAccountService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UmsShopAccount member = shopAccountService.getCurrentAccount();
        if (member==null){
            return true;
        }
        // 在处理请求之前进行操作，如鉴权、日志记录等
        if(member.getStatus() != CommonConstant.SUS_INT){
            String msg = "账号已被冻结";
            if(StringUtils.isNotBlank(member.getFreezeReason())){
                msg = msg + ",冻结原因:"+member.getFreezeReason();
            }
            throw new ServiceException(CommonConstant.WARN_MSG, msg);
        }
        return true; //返回true表示放行请求，false则不放行
    }
}